package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     *
     * @param page
     * @param rows
     * @param search
     * @param sortBy
     * @param desc
     * @return
     */
    @Override
    public PageResult<BrandDTO> queryForPage(Integer page, Integer rows, String search, String sortBy, Boolean desc) {
        PageHelper.startPage(page,rows);
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(search)){
            example.createCriteria().orLike("name","%"+search+"%")
                    .orEqualTo("letter",search);
        }

        if (StringUtils.isNotBlank(sortBy)){

            String orderByClause =sortBy+(desc ? " desc":" asc");
            example.setOrderByClause(orderByClause);
        }

        List<Brand> brands = brandMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(brands)){
            throw  new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        //通过pageInfo获取总条数,
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        return new PageResult<>(pageInfo.getTotal(),brandDTOS);
    }

    @Override
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.insertSelective(brand);
        if (count!=1){
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
//todo
        //如何获取brandid
        count = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if(count!=cids.size()){
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }

    @Override
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);

        // 修改品牌
        int count = brandMapper.updateByPrimaryKeySelective(brand);
        if(count != 1){
            // 更新失败，抛出异常
            throw new LyException(LyExceptionEnum.UPDATE_OPERATION_FAIL);
        }

        // 删除中间表数据
        brandMapper.deleteCategoryBrand(brand.getId());
        count = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if(count != cids.size()){
            // 新增失败，抛出异常
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public void deleteById(Long id) {
        int count = brandMapper.deleteByPrimaryKey(id);
        if(count != 1){
            // 删除失败,抛出异常
            throw new LyException(LyExceptionEnum.UPDATE_OPERATION_FAIL);
        }

        count = brandMapper.deleteCategoryBrand(id);
        if(count <1){
            //删除失败，抛出异常
            throw new LyException(LyExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }

    @Override
    public BrandDTO queryById(Long id) {
        Brand brand = new Brand();
        brand.setId(id);
        Brand brand1 = brandMapper.selectByPrimaryKey(brand);
        if (brand1==null){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand1,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryByCategoryId(Long cid) {
        List<Brand> list = brandMapper.queryByCategoryId(cid);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryByBrandIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(LyExceptionEnum.BRAND_NOT_FOUND);
        }
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(brandList, BrandDTO.class);


        return  brandDTOList;
    }
}
